#include <algorithm>
#include <cstdlib>  // Header file needed to use srand and rand
#include <iostream>
#include <stack>
#include <fstream>
#include <unordered_set>

#include"pulse_optical.h"
#include"pulse_optical.cc"
#include"graph.h"
#include"dijkstra.h"
#include"dijkstra.cc"

int main(){
    Graph test(9);
    
    std::ifstream Nodes("Nodes.txt", std::ios::in);
    if(!Nodes.is_open()){
		std::cout<<"Open file Nodes failed"<<std::endl;
		return 0;
	}else{
		std::cout<<"Open file Nodes successed"<<std::endl;
	}

    int node_id = 0;
    int type = 0;
    
    while(Nodes>>node_id){
        Nodes>>type;
        std::cout<<node_id<<type;
        test.addNodes(node_id, type);
    }
    Nodes.close();
    
    std::vector<int> v1 = {1, 2};
    std::vector<int> v2 = {2, 3};
    std::vector<int> v3 = {2};
    std::ifstream Links("Links.txt", std::ios::in);
     if(!Links.is_open()){
		std::cout<<"Open file Links failed"<<std::endl;
		return 0;
	}else{
		std::cout<<"Open file Links successed"<<std::endl;
	}

    int link_id =1;
    int source_id;
    int ter_id;
    double cost;
    double delay;
    int type_units;
    while(Nodes>>source_id){
        Nodes>>ter_id>>cost>>delay>>type_units;
        std::cout<<link_id<<source_id<<ter_id<<cost<<delay<<type_units;

        if(type_units==0){
            test.addLinks(link_id, source_id, ter_id, cost, delay, Omega);
            link_id++;

        }else if(type_units==1){
            test.addLinks(link_id, source_id, ter_id, cost, delay, v1);
            link_id++;     
        }else if(type_units==2){
            test.addLinks(link_id, source_id, ter_id, cost, delay, v2);
            link_id++;

        }else if(type_units==3){
            test.addLinks(link_id, source_id, ter_id, cost, delay, v3);
            link_id++;
        }
     
    }
    Links.close();

    //加入反向后的link
    test.addLinks(11, 2, 1, 2, 3, Omega);
    test.addLinks(12, 3, 1, 2, 1, v1);
    test.addLinks(13, 4, 2, 2, 1, v2);
    test.addLinks(14, 6, 3, 2, 1, Omega);
    test.addLinks(15, 5, 4, 5, 3, Omega);
    test.addLinks(16, 7, 4, 2, 2, v2);
    test.addLinks(17, 9, 4, 2, 1, v2);
    test.addLinks(18, 7, 6, 2, 4, v3);
    test.addLinks(19, 8, 7, 2, 2, v2);
    test.addLinks(20, 9, 8, 1, 1, Omega);

    test.makeGraph0();


   std::stack<Flow> flows_Dijfailed;

    std::ifstream Flows("Flows.txt", std::ios::in);
    if(!Flows.is_open()){
		std::cout<<"Open file Flows failed"<<std::endl;
		return 0;
	}else{
		std::cout<<"Open file Flows successed"<<std::endl;
	}

    int flow_id = 1;
    int from;
    int to;
    while(Flows>>from){
        Flows>>to;
        Flow test_flow(flow_id, from, to);
        dijkstra dij;
        dij.SetupTopology(&test);
        std::cout<<"dijkstra\n";
        Path result = dij.FindPath(test_flow);
        std::cout<<"dijkstra OK\n";
        if(result.cost>8){
            flows_Dijfailed.push(test_flow);
        }else{
        result.Print();
        }

        flow_id++;
    }
    Flows.close();
   
    
    return 0;
}